package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.urlrouter.UrlRouter;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.BannerLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.BannerItem;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBannerBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.retrofit.SelectedRequestParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

/**
 * Created by wcy on 18/5/14.
 */

public class AdSelectedHeaderLayout extends LinearLayout {

    @BindView(R.id.banner)
    BannerLayout mBanner;
    Unbinder mUnbinder;
    @BindView(R.id.second_entry_container)
    LinearLayout mSecondEntryContainer;
    int mType = Constants.BookCityTabType.TYPE_SELECTED;
    @BindView(R.id.banner_layout)
    FrameLayout mBannerLayout;
    SelectedRequestParams mParams;
    @BindView(R.id.indicators)
    LeanIndicatorContainer mIndicators;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();

    public AdSelectedHeaderLayout(Context context, int type) {
        super(context);
        mType = type;
        init();
    }

    public AdSelectedHeaderLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedHeaderLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.ad_selected_banner_header_layout, this);
        mUnbinder = ButterKnife.bind(view);
        mParams = new SelectedRequestParams();
        if (mType == Constants.BookCityTabType.TYPE_SELECTED) {
            DisplayUtils.visible(mSecondEntryContainer);
            mParams.type_name = Constants.BookCityBannerType.TYPE_SELECTED;
        } else {
            DisplayUtils.gone(mSecondEntryContainer);
            if (mType == Constants.BookCityTabType.TYPE_MALE) {
                mParams.type_name = Constants.BookCityBannerType.TYPE_MALE;
            } else {
                mParams.type_name = Constants.BookCityBannerType.TYPE_FEMALE;
            }
        }
    }

    @OnClick({R.id.ranking, R.id.category, R.id.end, R.id.topic})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ranking:
                AdPresenter.pvuvStatics(R.string.SCWAIST_LINE_POSITION1);
                ARouterUtils.toActivity(ARouterConfig.RANKING_LIST);
                break;
            case R.id.category:
                AdPresenter.pvuvStatics(R.string.SCWAIST_LINE_POSITION2);
                ARouterUtils.toActivity(ARouterConfig.CATEGORY_LIST);
                break;
            case R.id.end:
                AdPresenter.pvuvStatics(R.string.SCWAIST_LINE_POSITION3);
                ARouterUtils.toActivity(ARouterConfig.FINISHED_PAGE);
                break;
            case R.id.topic:
                AdPresenter.pvuvStatics(R.string.SCWAIST_LINE_POSITION4);
                ARouterUtils.toActivity(ARouterConfig.TOPIC_PAGE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //lifeCyclerSubject.onNext(ACTION_DESTORY);
    }
    /**
     * 刷新数据
     */
    public void refresh() {
        BookCityPresenter presenter = new BookCityPresenter(lifeCyclerSubject);
        presenter.getSelectedBanners(mParams, new INetCommCallback<SelectedBannerBean>() {
            @Override
            public void onResponse(SelectedBannerBean item) {
                mBannerLayout.setVisibility(VISIBLE);
                if (Utils.isEmptyList(item.list)) {
                    mBannerLayout.setVisibility(GONE);
                } else {
                    final List<String> list = new ArrayList<>();
                    for (BannerItem bannerItem : item.list) {
                        list.add(bannerItem.img_url);
                    }
                    mBanner.setViewUrls(list);
                    mIndicators.createIndicators(list.size());
                    mIndicators.setSelected(0);
                    mBanner.setPageListener(new ViewPager.SimpleOnPageChangeListener(){
                        @Override
                        public void onPageSelected(int position) {
                            super.onPageSelected(position);
                            mIndicators.setSelected(position);
                        }
                    });
                    mBanner.setOnBannerItemClickListener(position -> {
                        if (position >= list.size()) {
                            return;
                        }
                        if (mType == Constants.BookCityTabType.TYPE_SELECTED) {
                            AdPresenter.pvuvStatics(position,
                                    R.string.SCBANNER_POSITION1,
                                    R.string.SCBANNER_POSITION2,
                                    R.string.SCBANNER_POSITION3,
                                    R.string.SCBANNER_POSITION4,
                                    R.string.SCBANNER_POSITION5);
                        }

                        UrlRouter.from(getContext()).jump(item.list.get(position).jump_url);
                    });
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                mBannerLayout.setVisibility(VISIBLE);
            }
        });
    }


}
